package com.neu.dao.impl;

import com.neu.dao.BaseDao;
import com.neu.dao.FoodDao;

import java.util.List;
import java.util.Map;

public class FoodDaoImpl extends BaseDao implements FoodDao {
    @Override
    //查询所有食品
    public List<Map<String, Object>> findAll(String id) {
        List<Map<String,Object>> list = null;
        String sql = "SELECT food_id,food_name,food_explain,food_price,\n" +
                "CASE \n" +
                "\tWHEN food_status = 1 THEN '在售'\n" +
                "\tELSE '下架'\n" +
                "END food_status\n" +
                "FROM tbl_food\n" +
                "WHERE food_bussiness_id = ? ";
        list = executeQuery(sql,id);
        return  list;
    }

    @Override
    //按名称查询食品
    public List<Map<String, Object>> findByName(String name,String id) {
        List<Map<String, Object>> list = null;
        String sql = "SELECT food_id,food_name,food_explain,food_price,\n" +
                "CASE \n" +
                "\tWHEN food_status = 1 THEN '在售'\n" +
                "\tELSE '下架'\n" +
                "END food_status\n" +
                "FROM tbl_food\n" +
                "WHERE food_name LIKE ? AND food_bussiness_id = ?";
        list = executeQuery(sql,"%"+name+"%",id);
        return  list;
    }

    @Override
    //添加食品
    public int addFood(String name, String price, String info, String id) {
        int row = 0;
        String sql="INSERT INTO tbl_food(food_name,food_price,food_explain,food_bussiness_id)\n" +
                "VALUES(?,?,?,?)";
        row = super.executeUpdate(sql,name,price,info,id);
        return  row;
    }

    @Override
    //通过食品id查询食品
    public Map<String, Object> findById(String foodId, String id) {
        List<Map<String,Object>> list = null;
        Map<String,Object> map =null;
        String sql = "SELECT food_id,food_name,food_explain,food_price," +
                "CASE \n" +
                "\tWHEN food_status = 1 THEN '在售'\n" +
                "\tELSE '下架'\n" +
                "END food_status\n" +
                " FROM tbl_food Where food_id = ? and food_bussiness_id = ? and food_status = 1";
        list= super.executeQuery(sql,foodId,id);
        if (list.size()>0){
            map = list.get(0);
        }
        return map;
    }

    @Override
    public int deleteFood(String id) {
        int row = 0;
        String sql = "UPDATE tbl_food" +
                " SET food_status = 0" +
                " where food_id = ?";
        row = executeUpdate(sql,id);
        return row;
    }

    @Override
    public int editFood(String id, String name, String price, String explain, String status) {
        int row =0;
        String sql = "UPDATE tbl_food\n" +
                "SET food_name =?,\n" +
                "food_price = ?,\n" +
                "food_explain = ?,\n" +
                "food_status = ?\n" +
                "WHERE food_bussiness_id =?";
        row = super.executeUpdate(sql,name,price,explain,status,id);
        return row;
    }
}
